//
//  ViewController.m
//  22-ALiMusic
//
//  Created by dllo on 16/8/13.
//  Copyright © 2016年 ZHW. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "RootModel.h"
#import "Data.h"
#import "SmallData.h"
#import "SmallAction.h"
#import "CycleViewCollectionReusableView.h"
#import "TitleCollectionReusableView.h"
#import "MusicCollectionViewCell.h"
#import "LongCollectionViewCell.h"
#import "TuiJianCollectionViewCell.h"
#import "SongListCollectionViewCell.h"
#import "DetailViewController.h"
#define kCycleReusableView @"cycleReuse"
#define kCollectionViewReuse @"reuse"
#define kCollectionViewHead @"headReuse"
#define kMusicCollectionReuse @"musicReuse"
#define kLongCollectionReuse @"longReuse"
#define kTuijianCollectionReuse @"tuijianReuse"
#define kSongListCollectionReuse @"songListReuse"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) RootModel *rootModel;
@property (nonatomic, strong) UICollectionView *aLiCollectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.translucent = NO;
    [self handleData];
    [self creatCollectionView];
    fork();
}

- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        self.rootModel = [RootModel modelWithDic:dic];
        [_aLiCollectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"error = %@", error);
        
    }];
}

- (void)creatCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 3, 50);
    flowLayout.minimumLineSpacing = 5;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    
    self.aLiCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    _aLiCollectionView.backgroundColor = [UIColor colorWithRed:0.09 green:0.11 blue:0.14 alpha:1.00];
//    _aLiCollectionView.bounces = NO;
    _aLiCollectionView.delegate = self;
    _aLiCollectionView.dataSource = self;
    [self.view addSubview:_aLiCollectionView];
    [_aLiCollectionView registerNib:[UINib nibWithNibName:@"SongListCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kSongListCollectionReuse];
    [_aLiCollectionView registerNib:[UINib nibWithNibName:@"MusicCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kMusicCollectionReuse];
    [_aLiCollectionView registerNib:[UINib nibWithNibName:@"TuiJianCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kTuijianCollectionReuse];
    [_aLiCollectionView registerNib:[UINib nibWithNibName:@"LongCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kLongCollectionReuse];
    
    [_aLiCollectionView registerClass:[CycleViewCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCycleReusableView];
    [_aLiCollectionView registerNib:[UINib nibWithNibName:@"TitleCollectionReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHead];
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.valueString = _rootModel.dataArray[indexPath.section].data[indexPath.row].action.value;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(0, 150);
    } else if (section == 1) {
        return CGSizeZero;
    } else {
        return CGSizeMake(0, 50);
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        return CGSizeMake((CGRectGetWidth(self.view.bounds) - 15 ) / 2, 80);
    } else if (indexPath.section == 7) {
        return CGSizeMake(CGRectGetWidth(self.view.bounds), 80);
    } else  {
        return CGSizeMake((CGRectGetWidth(self.view.bounds) - 30) / 3, 100);
    }
}
#pragma mark - CollectionView DataSource
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        CycleViewCollectionReusableView *cycleHeadView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCycleReusableView forIndexPath:indexPath];
        cycleHeadView.smallDataArr = _rootModel.dataArray[indexPath.section].data;
        return cycleHeadView;
    } else {
        TitleCollectionReusableView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kCollectionViewHead forIndexPath:indexPath];
        headView.data = _rootModel.dataArray[indexPath.section];
        return headView;
    }
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _rootModel.dataArray.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
    return [_rootModel.dataArray[section] data].count;
    }
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        SongListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kSongListCollectionReuse forIndexPath:indexPath];
        cell.smallData = [_rootModel.dataArray[indexPath.section] data][indexPath.row];
        return cell;
    } else if (indexPath.section == 2) {
        TuiJianCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kTuijianCollectionReuse forIndexPath:indexPath];
        cell.smallData = [_rootModel.dataArray[indexPath.section] data][indexPath.row];
        return cell;
    } else if (indexPath.section > 2 && indexPath.section < 7) {
        MusicCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kMusicCollectionReuse forIndexPath:indexPath];
        cell.smallData = [_rootModel.dataArray[indexPath.section] data][indexPath.row];
        return cell;
    } else if (indexPath.section == 7) {
        LongCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kLongCollectionReuse forIndexPath:indexPath];
        cell.smallData = [_rootModel.dataArray[indexPath.section] data][indexPath.row];
        return cell;
    }
    else {
        UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewReuse forIndexPath:indexPath];
        cell.backgroundColor = [UIColor yellowColor];
        return cell;
    }
}

#pragma mark - Memory Warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
